/*
 * @Author: zhang zhijiang 2824735304@qq.com
 * @Date: 2025-05-20 20:05:36
 * @LastEditors: zhang zhijiang 2824735304@qq.com
 * @LastEditTime: 2025-05-20 20:12:38
 * @FilePath: /linux/minihttp/util.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
using namespace std;
class util
{
public:
   static  int read_line(int sock, string &out)
    {
        char ch = 'x';
        while (ch != '\n')
        {
            int s = recv(sock, &ch, 1, 0);
            if (s > 0)
            {
                if (ch == '\r')
                {
                    recv(sock, &ch, 1, MSG_PEEK);
                    if (ch == '\n')
                    {

                        recv(sock, &ch, 1, 0);//直接读取\n将\r覆盖
                    }
                    else
                    {
                        ch = '\n';
                    }
                }
                out.push_back(ch);
            }
            else if (s == 0)
            {
                return 0; // 对方终止通道
            }
            else
            {
                return -1;
            }
        }
        return out.size();
    }
};